//54 normal
/**
 * 数值还需微调
 * @param {number[][]} matrix
 */
function spiralOrder(
  matrix,
  begin = 0,
  row = matrix.length,
  column = matrix[0].length
) {
  if (row <= 0 || column <= 0) return [];
  let result = [];
  for (let i = 0; i < column; i++) {
    result.push(matrix[begin][i + begin]);
  }
  for (let i = 1; i < row; i++) {
    result.push(matrix[i + begin][column + begin - 1]);
  }
  if (row > 1) {
    for (let i = column + begin - 2; i >= begin; i--) {
      result.push(matrix[row - 1][i + begin]);
    }
  }
  if (column > 1) {
    for (let i = row + begin - 2; i > begin; i--) {
      result.push(matrix[i + begin][begin]);
    }
  }

  return result.concat(spiralOrder(matrix, begin + 1, row - 2, column - 2));
}
console.log(
  spiralOrder([
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16],
  ])
);
